import net.sf.emu.gs.ClientThread;
import net.sf.emu.gs.CommandHandler;
import net.sf.emu.gs.GameServerConfig;
import net.sf.emu.gs.muObjects.MuWorld;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class ApppMain extends Thread {
    private ServerSocket _serverSocket;
    private String _ip;

    public static int _port;

    public static void main(String[] args) throws Exception {

        CS CServer = new CS();
        ApppMain server = new ApppMain();
        FS FServer = new FS();

        System.out.println("GameServer Listening on port " + _port);
        CServer.start();//cs
        server.start();// runing GS
        FServer.start();//runing FS

    }

    /**
     *
     */
    public void run() {
        System.out.println("Init Reegions:...");
        MuWorld.getInstance().initRegions();
        System.out.println("Init Gates.....");
        MuWorld.getInstance().InitGates();
        CommandHandler.getInstancec();
        while (true) {
            try {

                Socket connection = _serverSocket.accept();
                ClientThread c = new ClientThread(connection);


            } catch (IOException e) {
                // not a real problem
            }
        }
    }

    public long getUsedMemoryMB() {
        return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime()
                .freeMemory()) / 1024 / 1024;
    }

    public ApppMain() throws Exception {
        super("AppMain");

        GameServerConfig.getInstance();

        _ip = GameServerConfig.GS_IP;
        _port = GameServerConfig.GS_PORT;

        System.out.println("used mem:" + getUsedMemoryMB() + "MB");

        String hostname = "*";

        if (!"*".equals(hostname)) {
            InetAddress adr = InetAddress.getByName(hostname);
            _ip = adr.getHostAddress();
            _serverSocket = new ServerSocket(_port, 50, adr);

        } else {
            _serverSocket = new ServerSocket(_port);

        }

        int maxPlayers = 10;
    }
}
